
const baseUrl = "https://api-hmugo-web.itheima.net/api/public/v1/"
//使用promise
let ajaxConter = 0
const request = function (opations) {
 ajaxConter++;
  wx.showLoading({
    title: '正在加载...',
    mask:true
  })
  if(opations.url.includes("my/")){
    opations.header = {
          ...opations.header,
          Authorization: wx.getStorageSync('token')
    };
}
      opations.url = baseUrl + opations.url;
      return new Promise((resolve, reject) => {
            wx.request({
                  ...opations,
                  timeout: 5000,
                  success: (result) => {
                        resolve(result)
                  },
                  fail: (err) => {
                        reject(err)
                  },
                  complete:()=>{
                    ajaxConter--;
                    if(ajaxConter === 0){
                      wx.hideLoading()
                    }
                    
                  }
            })
      })
}
const homeApi ={
  getSwiperData:function(){
    return request({
      url:"home/swiperdata",
    })
  },
  getCatitems:function(data){
    return request({
      url:"home/catitems"
    })
  },
  getFloordata:function(){
    return request({
      url:"home/floordata"
    })
  }
}
const categoriesApi = {
  getCategories:function(){
    return request({
      url:"categories"
    })
  }
};
const goodsApi ={
  // goods/search
  getGoodList:function(data){
    return request({
      url:"goods/search",
      data:data
    })
  },
  getGoodsDetail:function(data){
    return request({
          url: "goods/detail",
          data: data
    })
},
getGoodsSearch:function(data){
  return request({
        url: "goods/qsearch",
        data: data
  })
},
}
const usersApi ={
  wxlogin: function(data){
    return request({
          url: "users/wxlogin",
          method: "POST",
          data: data
    })
},
// my/orders/create
}
//定单接口
const myApi = {
  //创建订单接口
      /**
   * 1. 创建订单
   *    1. 请求地址 /my/orders/create
   *    2. 请求方式POST
   *    3. 请求参数
   *          1. 请求头 Authorization  token
   *          2. order_price,consignee_addr,goods
   */
  createOrder: function(data){
        return request({
              url: "my/orders/create",
              method: "POST",
              data: data
        })
  },
  //获取支付参数
  reqUnifiedorder: function(data){
        return request({
              url: "my/orders/req_unifiedorder",
              method: "POST",
              data: data
        })
  },
  //查看订单支付状态
  checkOrder: function(data){
        return request({
              url: "my/orders/chkOrder",
              method: "POST",
              data: data,
              header: {a:123}
        })
  },
}
const API ={
  homeApi,
  categoriesApi,
  goodsApi,
  usersApi,
  myApi
}
export default API
// export default request